package com.iraeta.service.iraorder.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.dao.iraorder.IraorderwuliustepspeedDao;
import com.iraeta.entity.Iraorderwuliustepspeed;
import com.iraeta.service.iraorder.IraorderwuliustepspeedService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IraorderwuliustepspeedServiceImpl implements IraorderwuliustepspeedService {
    @Resource
    IraorderwuliustepspeedDao iraorderwuliustepspeedDao;

    @Override
    public void addIraorderwuliustepspeed(Iraorderwuliustepspeed iraorderwuliustepspeed) {
        iraorderwuliustepspeedDao.insert(iraorderwuliustepspeed);
    }

    @Override
    public void updateIraorderwuliustepspeed(Iraorderwuliustepspeed iraorderwuliustepspeed) {
        iraorderwuliustepspeedDao.updateByPrimaryKey(iraorderwuliustepspeed);
    }

    @Override
    public void deleteIraorderwuliustepspeed(Integer id) {
        iraorderwuliustepspeedDao.deleteByPrimaryKey(id);
    }

    @Override
    public Iraorderwuliustepspeed getIraorderwuliustepspeed(Integer id) {
        return iraorderwuliustepspeedDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Iraorderwuliustepspeed> listIraorderwuliustepspeed(Integer wuLiuId) {
        return iraorderwuliustepspeedDao.selectByWuLiuId(wuLiuId);
    }

    @Transactional
    @Override
    public void updateIraorderwuliustepspeed(JSONArray stepList) {
        for(int i=0;i<stepList.size();i++){
            JSONObject stepJSONObject = stepList.getJSONObject(i);
            int id = stepJSONObject.getIntValue("id");
            int speed = stepJSONObject.getIntValue("speed");
            Iraorderwuliustepspeed iraorderwuliustepspeed = new Iraorderwuliustepspeed();
            iraorderwuliustepspeed.setId(id);
            iraorderwuliustepspeed.setSpeed(speed);
            this.updateIraorderwuliustepspeed(iraorderwuliustepspeed);
        }
    }
}
